package org.opencare.ndwc.messaging;

import java.util.Date;

import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Document;
import org.opencare.lib.model.Constants;
import org.opencare.lib.model.ExtensionFactory;
import org.opencare.lib.model.cap.Alert;
import org.opencare.lib.model.cap.Info;
import org.opencare.lib.model.cap.Resource;
import org.opencare.lib.model.edxl.ContentObject;
import org.opencare.lib.model.edxl.EDXLDistribution;
import org.opencare.lib.model.edxl.EmbeddedXMLContent;
import org.opencare.lib.model.edxl.XmlContent;

public class EDXLBuilderImpl implements Constants, EDXLBuilder {
	private Abdera abdera;
	private Factory factory;

	private String sender;
	private String fileUrl;
	private String fileDescription;
	
	public EDXLBuilderImpl() {
		super();
		// TODO Auto-generated constructor stub

		abdera = new Abdera();
		factory = abdera.getFactory();
		factory.registerExtension(new ExtensionFactory());
	}

	@Override
	public String build(String distributionID, String topic, String description, String eventType,
			String eventLevel, String disasterType, String[] mapPath, boolean isPublic){
		// TODO Auto-generated method stub

		Document<EDXLDistribution> doc = factory.newDocument();
			
		EDXLDistribution edxl = (EDXLDistribution) factory.newElement(EDXL_EDXL_DISTRIBUTION, doc);
		edxl.setDistributionID(distributionID);
		edxl.setSenderID(this.sender);
		edxl.setDateTimeSent(new Date());
						
		if( "ACTUAL".equalsIgnoreCase(eventType)){ 
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Actual);
		}else if("EXERCISE".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Exercise);
		}else if("SYSTEM".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.System);
		}else if("TEST".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Test);
		}		
				
		edxl.setDistributionType(EDXLDistribution.DistributionType.Report);
		
		edxl.setCombinedConfidentiality("UNCLASSIFIED AND NOT SENSITIVE");
		edxl.setLanguage("TH");
		
		/*edxl.addSenderRole("roleUrn1", "manager1");
		edxl.addSenderRole("roleUrn2", "manager2");
		edxl.addRecipientRole("roleUrn1", "staff1");
		edxl.addRecipientRole("roleUrn2", "staff2");
		edxl.addKeyword("keywordUrn1", "keyword1");
		edxl.addKeyword("keywordUrn2", "keyword2");
		edxl.addDistributionReference("ref-0001");
		edxl.addExplicitAddress("EA", "EA-0001", "EA-0002");

		TargetArea targetArea = edxl.addTargetArea();
		targetArea.addCircles("38.26295, -122.07454 15",
				"138.26295, 122.07454 50");
		targetArea
				.addPolygons(
						"42,-124.2102 42,-120.1 39,-120 35.0,-114.6328 34.35,- 120.4418 38.9383,-123.817 42,-124.2102",
						"10,-100.2102 42,-120.1 39,-120 35.0,-114.6328 34.35,- 120.4418 38.9383,-123.817 42,-124.2102");

		targetArea.addCountries("TH", "US");
		targetArea.addSubDivisions("US-TX", "DK-025");
		targetArea.addLocCodeUNs("USFFB", "USSUU");
		 */
		
		ContentObject contentObject = edxl.addContentObject();
		
		//contentObject.setContentDescription(description);
		/*contentObject.addContentKeyword("valueListUrn", "value");
		contentObject.setIncidentID("incident-001-1000");
		contentObject.setIncidentDescription("incident-desc");
		contentObject.addOriginatorRole("roleUrn", "manager");
		contentObject.addConsumerRole("roleUrn", "consumer");*/
		 
		XmlContent xmlContent = contentObject.setXmlContent();
		EmbeddedXMLContent embedded = xmlContent.addEmbeddedXMLContent();
		Alert alert = embedded.addAlert();

		alert.setIdentifier(distributionID);
		alert.setSender(this.sender);
		alert.setSent(new Date());
		
		if(isPublic) alert.setScope(Alert.Scope.Public);
		else alert.setScope(Alert.Scope.Restricted);
		
		alert.setType(Alert.Type.Alert);
		
		
				
		if( "ACTUAL".equalsIgnoreCase(eventType)){ 
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Actual);
			alert.setStatus(Alert.Status.Actual);
		}else if("EXERCISE".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Exercise);
			alert.setStatus(Alert.Status.Exercise);
		}else if("SYSTEM".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.System);
			alert.setStatus(Alert.Status.System);
		}else if("TEST".equalsIgnoreCase(eventType)){
			edxl.setDistributionStatus(EDXLDistribution.DistributionStatus.Test);
			alert.setStatus(Alert.Status.Test);
		}
				
		// ...
		Info info = alert.addInfo();
		info.setHeadline(topic);
		info.setDescription(description);
		info.setCategories(Info.Category.Geo);
		info.setEvent(disasterType);
		info.setUrgency(Info.Urgency.Past);
		info.setCertainty(Info.Certainty.Unknown);
		info.setSeverity(Info.Severity.Unknown);
		info.setLanguage("TH");
		info.addParameter("alertLevel", eventLevel);
		
		for(int i = 0; i < mapPath.length; i++){
			Resource resource = info.addResource();
			resource.setDescription("Map");
			resource.setUri(this.fileUrl + mapPath[0]);
		}
					
		//info.addEventCode("name", "value");

		// ...
		//Area area = info.addArea();
		/*area
				.addPolygon(
						"42,-124.2102 42,-120.1 39,-120 35.0,-114.6328 34.35,- 120.4418 38.9383,-123.817 42,-124.2102",
						"10,-100.2102 42,-120.1 39,-120 35.0,-114.6328 34.35,- 120.4418 38.9383,-123.817 42,-124.2102");*/
	
		StringBuffer buff = new StringBuffer();
		buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		buff.append(edxl.toString());
				
		return buff.toString();
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}

	public void setFileDescription(String fileDescription) {
		this.fileDescription = fileDescription;
	}

}
